package com.icarsaas.saas.component.base;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.icarsaas.saas.component.shiro.ShiroUtil;
import com.icarsaas.saas.tenant.system.model.User;
import io.swagger.annotations.ApiModelProperty;
import org.hibernate.validator.constraints.Length;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * 数据Entity基类(基础及安全框架基类)
 * @author xr
 * @version 2016-05-28
 */
@MappedSuperclass @EntityListeners(AuditingEntityListener.class)
public abstract class DataEntity implements Serializable {

	private static final long serialVersionUID = 1L;
	
	public static final String TOP_CODE = "001";
	public static final String SHOW = "1";
    public static final String HIDE = "0";
    // 是/否
    public static final String NO = "0";
    public static final String YES = "1";
	@ApiModelProperty("备注")
	@Column(columnDefinition="varchar(4000) COMMENT '备注'")
	protected String remarks;	// 备注
	/*
	@CreatedBy @ManyToOne(fetch = FetchType.LAZY) @NotFound(action = NotFoundAction.IGNORE)
	@JoinColumn(name = "create_by") @JsonIgnore
	private User createBy;
	*/
	@ApiModelProperty("创建者工号")
	@Column(name = "create_by", columnDefinition="varchar(30) COMMENT '创建者工号'")
	protected String createBy;	// 创建者
	@ApiModelProperty("创建者")
	@Column(name = "create_by_name", columnDefinition="varchar(50) COMMENT '创建者'")
	protected String createByName;	// 创建者姓名
	@ApiModelProperty("更新者工号")
	@Column(name = "update_by", columnDefinition="varchar(30) COMMENT '更新者工号'")
	protected String updateBy;	// 更新者
	@ApiModelProperty("更新者")
	@Column(name = "update_by_name", columnDefinition="varchar(50) COMMENT '更新者'")
	protected String updateByName;	// 更新者姓名
	@ApiModelProperty("创建时间")
	@Column(name = "create_date",columnDefinition="datetime COMMENT '创建时间'")
	@CreatedDate private Date createDate;
	@ApiModelProperty("更新时间")
	@Column(name = "update_date",columnDefinition="datetime COMMENT '更新时间'")
	@LastModifiedDate private Date updateDate;

	public DataEntity() {
		super();
	}
	
	@PrePersist
	public void prePersist() {
		User user = ShiroUtil.getUser();
        if(user != null && user.getUsername() != null) {
            this.updateBy = user.getUsername();
            this.updateByName = user.getNickname();
        } else {
            this.updateBy = "admin";
            this.updateByName = "超级管理员";
        }
        this.createBy = this.updateBy;
        this.createByName = this.updateByName;
	}
	
	@PreUpdate
	public void preUpdate() {
		User user = ShiroUtil.getUser();
        if(user != null && user.getUsername() != null) {
            this.updateBy = user.getUsername();
            this.updateByName = user.getNickname();
        } else {
        	this.updateBy = "admin";
        	this.updateByName = "超级管理员";
        }
	}
	
	@Length(min=0, max=4000)
	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	
	public String getCreateBy() {
		return createBy;
	}

	public void setCreateBy(String createBy) {
		this.createBy = createBy;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public String getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(String updateBy) {
		this.updateBy = updateBy;
	}

	public Date getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}

	public String getCreateByName() {
		return createByName;
	}

	public void setCreateByName(String createByName) {
		this.createByName = createByName;
	}

	public String getUpdateByName() {
		return updateByName;
	}

	public void setUpdateByName(String updateByName) {
		this.updateByName = updateByName;
	}

}
